<?php

namespace app\models;

use Yii;
use yii\base\Model;
use app\models\Proxy;
/**
 * LoginForm is the model behind the login form.
 *
 * @property User|null $user This property is read-only.
 *
 */
class ShareForm extends Model
{
    public $username;
    public $password;
    public $content;
    public $mobile;
    private $_user = false;

    /**
     * @return array attributeLabels.
     */
    public function attributeLabels(){
        return [
            'mobile' => '手机号',
            'username'=>'账户',
            'password'=>'密码',
            'content'=>'申请理由'
        ];

    }

    /**
     * @return array the validation rules.
     */
    public function rules()
    {
        return [
            // username and password are both required
            ['mobile', 'required','message'=>'手机号码不能为空'],
            ['mobile','match','pattern'=>'/^1[3|5|7|8]\d{9}$/','message'=>'请输入正确手机号码'],
            ['username', 'required','message'=>'用户名不能为空'],
            ['password', 'required','message'=>'密码不能为空'],
            ['content','required','message'=>'申请理由不能为空'],
            // rememberMe must be a boolean value
            ['password','validatePassword'],
        ];
    }

    /**
     * Validates the password.
     * This method serves as the inline validation for password.
     *
     * @param string $attribute the attribute currently being validated
     * @param array $params the additional name-value pairs given in the rule
     */
    public function validatePassword($attribute, $params)
    {
        if (!$this->hasErrors()) {
            $user = $this->getUser();
            if (!$user || !$user->validatePassword($this->password)) {
                $this->addError($attribute, '用户名或密码错误');
            }
        }
    }



    /**
     * Finds user by [[username]]
     *
     * @return User|null
     */
    public function getUser()
    {
        if ($this->_user === false) {
            $this->_user = User::findByUsername($this->username);
        }

        return $this->_user;
    }
}
